<?php
namespace Api\Common;

use Think\Controller;
use Api\Common\Secret;

class BaseController extends Controller
{

    public function __construct()
    {
       parent::__construct();
        // 解密验签
//       $secret = new Secret();
        if (!in_array(strtolower($_SERVER['HTTP_ORIGIN']),C("ALLOW_DOMAIN"))) {
            $this->forbidden();
        }
    }

    /**
     * 空操作
     */
    public function _empty()
    {
        $this->forbidden();
    }


    protected function forbidden()
    {
        $this->responseFail("forbidden");
    }

    /**
     * 成功的统一相应方法.
     * @param array   $data    相应数据.
     *
     * @return string
     */
    protected function responseSuccess( $data = array())
    {
        echo json_encode(array(
                    "code"    => 0,
                    "message" => "success",
                    "data"    => $data,
                ));
        exit();
    }

    /**
     * 成功的统一相应方法.
     * @param string  $message 相应信息.
     * @param array   $data    相应数据.
     *
     * @return string
     */
    protected function responseFail($message, $data = array())
    {
        echo json_encode(array(
            "code"    => 1,
            "message" => $message,
            "data"    => [],
        ));
        exit();
    }
}